package com.sky.controller.admin;

import com.sky.constant.JwtClaimsConstant;
import com.sky.dto.EmployeeDTO;
import com.sky.dto.EmployeeLoginDTO;
import com.sky.dto.EmployeePageQueryDTO;
import com.sky.dto.PasswordEditDTO;
import com.sky.entity.Employee;
import com.sky.properties.JwtProperties;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.EmployeeService;
import com.sky.utils.JwtUtil;
import com.sky.vo.EmployeeLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * 员工管理
 */
@RestController
@RequestMapping("/admin/employee")
@Slf4j
@Api(tags = "员工控制器")
public class EmployeeController {

    @Resource
    private EmployeeService employeeService;
    @Resource
    private JwtProperties jwtProperties;

    /**
     * 登录
     *
     * @param employeeLoginDTO
     * @return
     */
    @ApiOperation("登录请求")
    @PostMapping("/login")
    public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO) {
        log.info("[管理端员工登录] 开始执行");

        Employee employee = employeeService.login(employeeLoginDTO);

//        log.info("[登录请求] 登录成功,生成JWT令牌");

        //TODO 把以下生成JWT令牌的代码放到EmployeeServiceImpl中,让employeeService.login()方法返回值直接设置为EmployeeLoginVO
        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.EMP_ID, employee.getId());
        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claims);

//        log.info("[登录请求] 封装VO对象");

        /**
         * @Builder: 是lombok提供的注解,能让我们用以下这种方式来创建对象,提高代码可读性
         */
        EmployeeLoginVO employeeLoginVO = EmployeeLoginVO.builder()
                .id(employee.getId())
                .userName(employee.getUsername())
                .name(employee.getName())
                .token(token)
                .build();

        log.info("[管理端员工登录] 执行完毕");
        return Result.success(employeeLoginVO);
    }

    /**
     * 登出
     *
     * @return
     */
    @PostMapping("/logout")
    @ApiOperation("登出请求")
    public Result<String> logout() {
        return Result.success();
    }

    /**
     * 增
     *
     * @param employeeDTO
     * @return
     */
    @PostMapping
    @ApiOperation("新增员工")
    public Result<String> add(@RequestBody EmployeeDTO employeeDTO){
        log.info("[新增员工] 开始执行");
        Integer tag = employeeService.add(employeeDTO);
        if(tag>0){
            log.info("[新增员工] 新增成功");
            return Result.success();
        }else{
            log.info("[新增员工] 新增失败");
            return Result.error("添加失败");
        }
    }

    /**
     * 查(分页查)
     *
     * @param employeePageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("分页查询")
    public Result<PageResult> findAll(EmployeePageQueryDTO employeePageQueryDTO){
        log.info("[分页查询] 开始执行");
        PageResult pageResult = employeeService.findAll(employeePageQueryDTO);
        /*List records = pageResult.getRecords();
        long total = pageResult.getTotal();*/
        log.info("[分页查询] 执行完毕");
        return Result.success(pageResult);
    }

    /**
     * 启用/禁用员工状态
     *
     * @param id
     * @param status
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation("启用/禁用员工状态")
    public Result<String> setStatus(Long id,@PathVariable Integer status){
        log.info("[更改员工状态] 开始执行");
        Integer tag = employeeService.modifyStatus(id,status);  //此处status是员工更改后的状态
        if(tag>0){
            log.info("[更改员工状态] 更改成功");
            return Result.success();
        }else{
            log.info("[更改员工状态] 更改失败");
            return Result.error("更改员工状态失败");
        }
    }

    /**
     * 查(id查)
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查员工")
    public Result<Employee> findById(@PathVariable Long id){
        log.info("[根据id查询员工] 开始执行");
        Employee emp=employeeService.findById(id);
        if(emp==null){
            log.info("[根据id查询员工] 查询失败");
            return Result.error("根据id查询员工失败");
        }else{
            log.info("[根据id查询员工] 查询成功");
            return Result.success(emp);
        }
    }

    /**
     * 改
     *
     * @param employeeDTO
     * @return
     */
    @PutMapping
    @ApiOperation("修改员工")
    public Result<String> modify(@RequestBody EmployeeDTO employeeDTO){
        log.info("[修改员工] 开始执行");
        Integer tag = employeeService.modify(employeeDTO);
        if(tag>0){
            log.info("[修改员工] 修改成功");
            return Result.success();
        }else{
            log.info("[修改员工] 修改失败");
            return Result.error("员工修改失败");
        }
    }

    /**
     * 改(密码)××××前端传来的数据有问题,但通过后端解决了××××
     *
     * @param passwordEditDTO
     * @return
     */
    @PutMapping("/editPassword")
    @ApiOperation("修改密码")
    public Result<String> modifyPwd(@RequestBody PasswordEditDTO passwordEditDTO){
        log.info("[修改密码] 开始执行");
        Integer tag = employeeService.modifyPwd(passwordEditDTO);
        if(tag>0){
            log.info("[修改密码] 修改成功");
            return Result.success();
        }else if(tag==0){
            log.info("[修改密码] 修改失败");
            return Result.error("密码修改失败");
        }else{
            log.info("[修改密码] 原密码输入错误");
            return Result.error("原密码输入错误");
        }
    }
}